$(function(){
	var ruleId = getUrlParam('ruleId');
	initForm(ruleId);
	bindDict("remindtype", "remind_type", null);
	bindDict("remindrateunit","TIME_UNIT",null);
	bindDict("remindruletype","SUP_RULE",null);
	
})

function initForm(ruleId){
	$.ajax({ 
		type:'get',
		url: projectPath + "supervise/getInfo.do",
		data: {"ruleId": ruleId},
		dataType: "json",
		success: function(data){
			if(data.result == 1){ 
				data.datas.ruleid = ruleId;
				bindFormDictInfo(data.datas);
			}
      	}
	});
}

function bindFormDictInfo(data){
	$('[name="ruleid"]').val(data.ruleid);
	$('[name="itemno"]').val(data.itemno);
	$('[name="itemname"]').val(data.itemname);
	$('[name="remark"]').val(data.remark);
	$('[name="remindprescription"]').val(data.remindprescription);
	$('[name="remindtype"] option[value="'+data.remindtype+'"]').attr("selected",true);
	$('[name="remindruletype"] option[value="'+data.remindruletype+'"]').attr("selected",true);
	$('[name="remindrule"]').val(data.remindrule);
	$('[name="remindrate"]').val(data.remindrate);
	$('[name="remindrateunit"] option[value="'+data.remindrateunit+'"]').attr("selected",true);
	if(data.status  == 1){
		$('[name="status"]:eq(0)').attr("checked",true); 
	}else{
		$('[name="status"]:eq(1)').attr("checked",true); 
	}
	
}



function save(){
	var ruleData = $("#ruleForm").serializeObject();
	if(ruleData.status == undefined){
		ruleData['status'] = 0;
	}
	 
	console.log(JSON.stringify(ruleData));

	if(!check(ruleData))
		return true;
	
	$.ajax({ 
		method: 'post',
		url: basePath() + 'supervise/update.do',
		data: JSON.stringify(ruleData),
		dataType: "json",
        contentType: "application/json",
		success: function(data){	
	    	window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.success, {
	    		onOk: function(){
	    			if(data.result == 1){
	    				parent.refreshData();		//刷新数据
	    			} 
	    		}
	    	});
      	}
	});
}


function check(data){	
	var result = true;
	
	
	var mustStr = "itemno,remindtype,remindruletype,remindrule,remindprescription,remindrateunit";
	
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for(var i=0; i<mustStrs.length; i++){
		var obj = $('[name="'+mustStrs[i]+'"]');
		if(obj.val() == ""){
			strMsg += obj.attr("title") + "！<br />";
			
			result = false;
		}
	}
	
	if(strMsg != ""){
		window.wxc.xcConfirm(strMsg, window.wxc.xcConfirm.typeEnum.error);
	}
	
	return result;
}
 
